#include <stdio.h>  
 
// 电压转换器的接口  
typedef struct VoltageConverter{  
    int input_voltage; // 输入电压  
    int target_voltage; // 目标电压
    void (*convert)(struct VoltageConverter *converter, int input);  
	void (*add)(struct VoltageConverter *converter);  
	void (*reduce)(struct VoltageConverter *converter);  
	void (*oprate)(struct VoltageConverter *converter);
} VoltageConverter;

// 电压转换器的实现  
void add_voltage(VoltageConverter *converter) {  
    while(converter->input_voltage < converter->target_voltage)
    {  
		converter->input_voltage+=1;  
		printf("Converting %dV\n", converter->input_voltage);  
	}
}  
  
void reduce_voltage(VoltageConverter *converter) {  
    while(converter->input_voltage > converter->target_voltage)
    {  
		converter->input_voltage-=1;  
		printf("Converting %dV\n", converter->input_voltage);  
	}
} 
  
// 电压转换器的实现  
void convert_voltage(VoltageConverter *converter, int input_voltage) {  
    converter->input_voltage = input_voltage;  
    if(converter->input_voltage > converter->target_voltage ) 
	{
		converter->reduce(converter);
	}
	else
	{
		converter->add(converter);
	}
	converter->oprate(converter);
}  
  
// 设备的实现  
void operate_device(VoltageConverter *converter) {  
    printf("Device: Operating with %dV\n", converter->input_voltage); 
}  
  
// 主函数，演示如何使用这些接口  
int main() {  
     
    VoltageConverter converter = {220, 12, convert_voltage,add_voltage,reduce_voltage,operate_device};  
      
    // 使用电压转换器  
    converter.convert(&converter, 20);  
	
	// 使用电压转换器  
    converter.convert(&converter, 3);  
  
  
    return 0;  
}